package com.hui;

import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author zhang zhongtao
 */
@Configuration
@EnableBatchProcessing
@EnableAutoConfiguration
@Slf4j
public class BatchConfiguration {

    /*自动注入Step的工厂类，用于生成step*/
    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    /*自动注入Job的工厂类，用于生成job*/
    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Bean
    public Step step1(){
        System.out.println("[This is springbatch-learn-6_step1]");
        Step step = stepBuilderFactory.get("springbatch-learn-6_step1").tasklet((contribution, chunkContext) -> {
            System.out.println("springbatch-learn-6_step1 process...");
            return RepeatStatus.FINISHED;
        }).build();
        return step;
    }

    @Bean
    public Step step2(){
        System.out.println("[This is springbatch-learn-6_step2]");
        Step step = stepBuilderFactory.get("springbatch-learn-6_step2").tasklet((contribution, chunkContext) -> {
            System.out.println("springbatch-learn-6_step2 process...");
            return RepeatStatus.FINISHED;
        }).build();
        return step;
    }

    /*
    on(ExitStatus.COMPLETED.getExitCode())表明如果step2步骤执行完的状态为COMPLETED，由to(step1())指定跳转到step1步骤；
    end()为结束跳转；
    */
    @Bean
    public Job job1(){
        Job job = jobBuilderFactory.get("springbatch-learn-6_job1")
                .start(step2())
                .on(ExitStatus.COMPLETED.getExitCode())
                .to(step1())
                .end()
                .build();
        return job;
    }


}
